Skip to content

perf: eliminate TimedBatch deadline-wait allocations (AsTask + WhenAny) — issue-204 - #205

Merged
SunSi12138 merged 6 commits into
devfrom
feature/issue-204-timedbatch-alloc
Aug 15, 2026
Merged

perf: eliminate TimedBatch deadline-wait allocations (AsTask + WhenAny) — issue-204#205
SunSi12138 merged 6 commits into
devfrom
feature/issue-204-timedbatch-alloc

Conversation

@SunSi12138

Copy link
Copy Markdown
Owner

Closes #204.

问题

SendPumpTimedBatchDeadlineCycle 在 deadline 等待周期分配 566 B/cycle(issue #204 数据 547-576 区间),远高于正常 idle→wake 周期的 496 B。开销来自 WaitForMoreUntilDeadlineAsync 里的:

  • WaitToReadAsync(_sessionCancellation):可取消 token 使 unbounded channel 无法复用池化 waiter,每次分配 WaitingReadAsyncOperation + CancellationTokenRegistration(.NET 10 UnboundedChannel.WaitToReadAsync 源码确认:CanBeCanceled == false 才走 _waiterSingleton.TryOwnAndReset() 零分配路径)
  • waitToRead.AsTask():分配 ValueTaskSourceAsTask(channel 的 AsyncOperation 不实现 ITaskCompletionActionValueTask<bool> 无公开 OnCompleted,这一步无法省)
  • Task.Delay(...)DelayPromise + 每次一个 timer
  • Task.WhenAny(...)WhenAnyPromise
  • 周期外的 CancellationTokenSource 复用/取消逻辑

方案

新增 DeadlineReadRaceIValueTaskSource<bool>,内嵌 ManualResetValueTaskSourceCore<bool>):

  • 读侧改为 WaitToReadAsync(CancellationToken.None)(池化 waiter,与 perf: non-cancellable SendPump channel waits (issue 157) #203 主等待路径一致)
  • 每周期仅剩:AsTask() 一次 + 每次等待一个 TimeProvider.CreateTimer(tick 精确,超时即 Dispose)+ 一个闭包
  • 彻底移除 Task.WhenAnyTask.Delay_delayCancellation CTS

保留的语义(#157 约束)

  • timer 赢后 pending read 不消费、由 _pendingReadWait 保留并在下一轮 WaitToReadAsync() 复用
  • read 赢 → 继续合批(batchDeadline 不重置);writer 关闭 → ReadClosed 清保留退出;超长 deadline 按 MaximumTimerDelay 分块重挂
  • 不改 batching policy、不动 force-flush 语义

并发正确性(关键点)

  • timer 先于 read continuation 注册创建(UnsafeOnCompleted 对已完成任务会内联同步调用)
  • 每次 arm 用闭包捕获自身 read + ReferenceEquals 身份校验:上一轮的迟到 continuation 不可能作用到当前轮状态
  • read/timer 互斥由 Interlocked abandon 标志裁决;RunContinuationsAsynchronously 防重入

验证(192.168.31.242,SDK 10.0.110,taskset -c 2,3)

TimedBatchDeadlineCycle baseline (dev) 本 PR Δ
Allocated 566 B 363 B -36%
Mean 5.855 μs 2.710 μs -54%
Gen0 /1000op 0.0305 0.0153 -50%
Op/s 170,788 369,025 +116%
  • deadline 周期分配现低于 idle→wake 的 496 B;idle-wake 4 场景两树均 496 B,无回退
  • 隔离 probe(泵形态周期,20 万轮):deadline-wait 机制 456 → 240 B/cycle(-47%),Gen0 6 → 3

测试

新增 4 例 bounded-completion 测试(SendPumpTests):

  • TimedBatchShouldDeliverFrameSentAfterDeadlineFlushThroughRetainedRead —— 保留语义回归(timer 赢后读复用;丢弃读会使后续帧永远无法唤醒泵)
  • TimedBatchShouldExtendBatchForFrameArrivingBeforeDeadline —— read 赢合批(两帧一次 flush,记录型 provider 同步)
  • TimedBatchShouldRearmAcrossMaximumTimerDelayChunks —— 超长 deadline 分块重挂且不提前 flush
  • TimedBatchDeadlineWaitShouldExitWhenSessionIsDisposed —— 等待期停止(ReadClosed)

远程 943 总/942 过:唯一失败为 PumpBlockedInFlushExitsWhenTransportOutputFaults——dev 既有问题(隔离复跑 5/5 确定性 5s 超时,本改动不触及 Balanced flush 故障路径;本地 macOS 全套 943/943 全绿)。CI 上 ManifestSourceIsolationTests 的 poison flake 为已知既有竞态。

sunsi added 2 commits August 15, 2026 13:25
@SunSi12138

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 2b1f7f582d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/SharpLink.Runtime/DeadlineReadRace.cs Outdated
Comment thread test/SharpLink.UnitTests/Runtime/SendPumpTests.cs Outdated
…st timer arms after install (issue-204 codex review)
@SunSi12138

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 443f4f731a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/SharpLink.Runtime/DeadlineReadRace.cs Outdated
Comment thread src/SharpLink.Runtime/DeadlineReadRace.cs Outdated
sunsi added 2 commits August 15, 2026 19:31
…mers before arming (issue-204 codex review round 2)
@SunSi12138

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 95e33bbe83

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread test/SharpLink.UnitTests/Runtime/SendPumpTests.cs Outdated
@SunSi12138

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: eb948c7837

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant